const pool = require("../connection/sqlPool");

//获取用户基础信息
async function GETMESSAGE (username){
    const promisePool = pool.promise();
    const [rows,fileds] = await promisePool.query('SELECT * FROM `user_message` WHERE `username` = ?',[username]);
    //console.log(rows);
    return rows[0]
}

//获取用户对战次数
async function GETHISTORY_total(username){
    const promisePool = pool.promise();
    const [rows,fileds] = await promisePool.query('SELECT * FROM `game_history` WHERE `usernameR` = ? OR `usernameB` = ?',[username,username]);
    return rows
}

//获取用户赢得对战次数
async function GETHISTORY_win(username){
    const promisePool = pool.promise();
    const [rows,fileds] = await promisePool.query('SELECT * FROM `game_history` WHERE `win`=?',[username]);
    return rows
}

async function GETRECITE(username){
    const promisePool = pool.promise();
    const [rows,fileds] = await promisePool.query('SELECT * FROM `Word_recite` WHERE `username`=?',[username]);
    return rows;    
}

async function GETRECITE_firm(username){
    const promisePool = pool.promise();
    const [rows,fileds] = await promisePool.query('SELECT * FROM `Word_recite` WHERE `username`=? AND `phase`>=70',[username]);
    return rows;    
}

async function GETRECITE_min(username){
    const promisePool = pool.promise();
    const [rows,fileds] = await promisePool.query('SELECT * FROM `Word_recite` WHERE `username`=? ORDER BY `phase` ASC LIMIT 1',[username]);
    return rows;    
}

async function GETDIFFWORD(word_id){
    const promisePool = pool.promise();
    const [rows,fileds] = await promisePool.query('SELECT * FROM `Word` WHERE `id`=?',[word_id]);
    return rows[0].word;
}

async function GETFRIENDS(username){
    const promisePool = pool.promise();
    const [rows,fileds] = await promisePool.query('SELECT * FROM `Friends` WHERE `host`=?',[username]);
    return rows;
}

module.exports={GETMESSAGE,GETHISTORY_total,GETHISTORY_win,GETRECITE,GETRECITE_firm,GETRECITE_min,GETDIFFWORD,GETFRIENDS};